package com.mobinters.cafeman.adapter;

import java.util.ArrayList;
import java.util.List;

import com.mobinters.cafeman.R;
import com.mobinters.cafeman.object.Product;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;

public class CProductAdapter extends BaseAdapter {

	Context mContext;
	LayoutInflater mInflater;
	List<Product> mData;
	public List<Product> mFullData;
	
	public CProductAdapter(Context context,List<Product> data){
		mContext=context;
		mFullData=data;
		
		mInflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		getCheckedList();
	}
	
	/*
	 * if there is any checked item --> return true --> display listview
	 * else --> return false --> invisible listview
	 */
	public void getCheckedList(){
		if(mFullData == null){
				mData = null;
				return;
		}
		mData = new ArrayList<Product>();
		Product item;
		for(int  i = 0; i < mFullData.size() ; i++){
			item = mFullData.get(i);
			if(item.Checked) mData.add(item);
		}
	}
	
	public List<Product> getCheckedData(){
		return mData;
	}
	public boolean isCheckedList(){
		if(mFullData == null) return false;
		Product item;
		for(int  i = 0; i < mFullData.size() ; i++){
			item = mFullData.get(i);
			if(item.Checked) return true;
		}
		return false;
	}
	
	@Override
	public void notifyDataSetChanged() {
		// TODO Auto-generated method stub
		getCheckedList();
		super.notifyDataSetChanged();
		
		
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup viewgroup) {
		// TODO Auto-generated method stub
		if(view == null){
			view = mInflater.inflate(R.layout.slide_listview_item, viewgroup, false);
		}
		Product item = mData.get(position);
		
		TextView tvName = (TextView) view.findViewById(R.id.txt_slide_item_name);
		tvName.setText(item.ProductName1);
		
		CheckBox cb = (CheckBox) view.findViewById(R.id.cb_slide_item);
		cb.setChecked(item.Checked);
		
		Spinner spinNumber = (Spinner) view.findViewById(R.id.spin_slide_item_number);
		
		spinNumber.setSelection(item.Number);
		
		spinNumber.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View arg1,
					int sposition, long arg3) {
				// TODO Auto-generated method stub
				int id = mData.get(position).ProductId;
				Product item = findItemById(id);
				if(item !=null) {
					String value = parent.getItemAtPosition(sposition).toString();
					item.Number = Integer.parseInt(value);					
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
		
		return view;
	}

	
	private Product findItemById(int id){
		for(Product item: mFullData){
			if(item.ProductId == id) return item;
		}
		return null;
	}
}
